package jwdt.xadmin.work.service.impl;

import java.io.File;

import javax.servlet.ServletContext;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.system.bean.TUser;
import jwdt.xadmin.work.bean.TDoc;
import jwdt.xadmin.work.service.DocManageService;

import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.web.ServletActionContext;
import com.ezerg.web.fileupload.FileItem;

public class DocManageServiceImpl implements DocManageService{

	private BaseDao<TDoc> baseDao;
	
	public void setBaseDao(BaseDao<TDoc> baseDao) {
		this.baseDao = baseDao;
	}
	
	private String webPath = "WEB-INF/files/docs/";
	
	@Override
	public PageUtil doList(PageUtil pu,TUser user,TDoc doc) throws Exception{
		String sql = " 1=1 ", sql_order = " order by dt desc";
		String name = doc.getName();
		Integer userid = doc.getUserid();
		Integer classid = doc.getClassid();
		Integer zt = doc.getZt();
		if(name != null && !"".equals(name)){
			sql += " and name like '"+name+"'";
		}
		if(userid !=null&&userid !=0){
			sql += " and userid="+userid;
		}
		if(classid != null && classid != 0) {
			sql += " and classid="+classid;
		}
		if(zt!=null){
			if(zt==0){
				sql += " and userid="+userid;
			}else if(zt==1){
				sql += " and zt="+zt;
			}else if(zt==2){
				sql += " and zt="+zt+" and depid="+user.getDepid();
			}
		}
		pu.doList(TDoc.class,sql+sql_order);
		//创造json数据对象
		return pu;
	}
	
	@Override
	public String doAddact(TDoc obj,FileItem file,TUser user) throws Exception {
		ServletContext sc = ServletActionContext.getServletContext();
		String basePath = sc.getRealPath("/");
		String uploadPath = basePath.replace("\\", "/") + webPath;
		File up = new File(uploadPath + user.getUsername()+"/");
		String path = webPath + user.getUsername()+"/";
		try {
			if(!up.exists()) {
				up.mkdirs();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String ret = "添加成功";
		try {
			if(file != null) {
				String fileExt = file.getFileExt();
				String fileName = obj.getDocid() + "." + fileExt;
				file.write(up +"/"+ fileName);
				obj.setContent(path+fileName);
			}else{
				ret = "请上传文件";
			}
			obj.setUserid(user.getUserid());
			obj.setDeptid(user.getDepid());
			obj.setDt(DateUtil.getDateTime());
			ret = baseDao.doAddObj(obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "添加出错";
		}
		return ret;
	}

	@Override
	public String doEditact(TDoc obj, int id,FileItem file,TUser user) throws Exception {
		ServletContext sc = ServletActionContext.getServletContext();
		String basePath = sc.getRealPath("/");
		String uploadPath = basePath.replace("\\", "/") + webPath;
		File up = new File(uploadPath + "/" + user.getUsername()+"/");
		String path = webPath + user.getUsername()+"/";
		try {
			if(!up.exists()) {
				up.mkdirs();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		String ret = "修改成功";
		try {
			if(file != null) {
				String fileExt = file.getFileExt();
				String fileName = obj.getDocid() + "." + fileExt;
				file.write(up +"/"+ fileName);
				obj.setContent(path+fileName);
			}else{
				ret = "请上传文件";
			}
			obj.setUserid(user.getUserid());
			obj.setDeptid(user.getDepid());
			obj.setDt(DateUtil.getDateTime());
			ret = baseDao.doUpdateObj(TDoc.class, id, obj);
		} catch (Exception e) {
			e.printStackTrace();
			ret = "修改出错";
		}
		return ret;
	}
	
	@Override
	public String doDelact(int id) throws Exception {
		return baseDao.doDelObj(TDoc.class, id);
	}

	@Override
	public TDoc doLoad(int id) throws Exception {
		return baseDao.doLoadObj(TDoc.class, id);
	}
	
	@Override
	public String doUpdateState(int id, String result) throws Exception {
		return baseDao.doIntUpdateState(TDoc.class, "docid="+id, result);
	}
}
